﻿﻿$('.nav-btn.login-part').on('tap', function() {
	$('.nav-btn').removeClass('mui-active');
	$(this).addClass('mui-active');
	$('#reg').fadeOut('fast');
	$('#login').fadeIn('fast');
});

$('.nav-btn.reg-part').on('tap', function() {
	$('.nav-btn').removeClass('mui-active');
	$(this).addClass('mui-active');
	$('#login').fadeOut('fast');
	$('#reg').fadeIn('fast');
});
$('#loginBtn').on('tap', function() {
	if (App.checkPattern($('#account').val(),App.mobileReg)) {
		console.log('号码格式正确');
		
		if( $('#password').val() ){
			var params = {
				mobile : $('#account').val(),
				password : $('#password').val(),
			};
			localStorage.userAccount = JSON.stringify(params);
			$('#loadingPart').show();
			App.appLogin(params,function(data){
				console.log(JSON.stringify(data) );

				if (data.result) {
				    localStorage.sysChildrenId = data.data.sysChildrenId;
//					mui.toast(data.msg);
					var userInfo = {
						mobile : params.mobile,
						password: params.password,
						sysChildrenId: data.data.sysChildrenId,
						trueName : typeof(data.data.trueName) ? data.data.trueName : null
					}
					localStorage.userInfo = JSON.stringify(data.data);
					if(typeof(data.data.trueName) == 'undefined'){
						$('#loadingPart').hide();
						//未完善资料进personSafe
						personSafePage = mui.preload({
							url: 'personSafe.html',
							id: 'personSafe',
							styles: {
							}
						});
						setTimeout(function(){
							App.showPage('personSafe');
						},500);
					}
					else{
						//登录检查进index
						indexPage = mui.preload({
							url: 'index.html',
							id: 'index',
							styles: {
							}
						});
						setTimeout(function(){
							$('#loadingPart').hide();
							App.showPage('index');
						},1000);
					}

				}
				else{
					$('#loadingPart').hide();
					mui.toast(data.msg);
				}
			});
		}

	}
	else{
		mui.toast('账号格式不正确！');
	}

});

$('.reg-part').on('tap', function() {
	$('#reg').addClass('mui-active');
	$('#reg').addClass('mui-active');
});

$('#identify').on('tap',function(){
	if (App.checkPattern($('#RegAccount').val(),App.mobileReg)) {
		console.log('号码格式正确');
		var params = {
			mobile : $('#RegAccount').val()
		};
		App.sendMobileCode(params,function(data){
			console.log(JSON.stringify(data) );
			if (data.result) {
				$('#identifyNum').val(data.data.mobileCode);
			}
		});
	}
	else{
		mui.toast('号码格式不正确！');
	}
});

//注册按钮  检查格式
$('#regBtn').on('tap',function(){
	if ($('#RegPassword').val() == $('#RegRePassword').val() ){
		if (App.checkPattern($('#RegAccount').val(),App.mobileReg)) {
			if ($('#identifyNum').val().length == 6) {
				var params = {
					mobile : $('#RegAccount').val(),
					mobileCode : $('#identifyNum').val(),
					password: $('#RegPassword').val()
				};
				App.appRegister(params,function(data){
					console.log(JSON.stringify(data) );
					if (data.result) {
						mui.toast(data.msg);
						$('#account').val(params.mobile);
						$('#password').val(params.password);
						var userAccount = {
							mobile : params.mobile,
							password : params.password
						}
						localStorage.userAccount = JSON.stringify(userAccount);
						setTimeout(function(){
							$('.nav-btn').removeClass('mui-active');
							$('.nav-btn.login-part').addClass('mui-active');
							$('#reg').fadeOut('fast');
							$('#login').fadeIn('fast');
						},800);
					}
					else{
						$('#loadingPart').hide();
						mui.toast(data.msg);
					}
				});
			}
			else{
				mui.toast('验证码输入错误！');
			}
		}
		else{
			mui.toast('号码格式不正确！');
		}
	}
	else{
		mui.toast('2次密码不相同！');
	}
});

//进忘记密码界面
$('#forgetPassword').on('tap', function() {
	//登录检查进index
	resetPasswordPage = mui.preload({
		url: 'resetPassword.html',
		id: 'resetPassword',
		styles: {

		}
	});
	setTimeout(function(){
		App.showPage('resetPassword');
	},200);
});

//下载
function DownLoadFile(url) {
		console.log(url)
//		plus.nativeUI.showWaiting("下载升级文件...");
		var dtask = plus.downloader.createDownload( url, {method:"GET"}, function ( d, status ) {
            // 下载完成
//          plus.nativeUI.closeWaiting();
            $('#loadingPart').show();
            if ( status == 200 ) {
//              alert(d.filename);
                plus.runtime.install(d.filename, {force:true}, function() {
                			$('#loadingPart').hide();
                        //完成更新向服务器进行通知
                        mui.toast("更新完毕，将重启应用！");
                        plus.runtime.restart();
                    },function(err){
                    		$('#loadingPart').hide();
                        alert(JSON.stringify(err));
                        mui.toast("安装升级失败");
                    });
            } else {
            		 $('#loadingPart').hide();
                 alert( "Download failed: " + status );
            }
		});
		 dtask.addEventListener('statechanged',function(d,status){
	        console.log("statechanged: "+d.state);
	    });
	    dtask.start();
}


$(function() {　
	if (typeof(localStorage.userAccount)!="undefined") {
		var userAccount = JSON.parse(localStorage.userAccount);
		$('#account').val(userAccount.mobile);
		$('#password').val(userAccount.password);
	}
	mui.plusReady(function() {
		plus.runtime.getProperty(plus.runtime.appid,function(inf){
	        //wgtVer=inf.version;
	        console.log("当前应用版本："+inf.version+"---"+plus.runtime.version);
	        var params ={
	        	ep_app_platform : App.ep_app_platform,
				epAppType : App.epAppType,
				innerVersion : inf.version,
				epAppVersion : plus.runtime.version
			}
			if(plus.os.name != "iOS"){
			    App.versionCheck(params,function(data){
                	        console.log(JSON.stringify(data));
                	        	//有增量包  增量更新
                	        	if (typeof(data.data)!="undefined") {
                	        		//IOS整包更新
//                	        		if(!(data.data.epAppType == "IOS" && data.data.updateType != "pageket")){
                	        			if(confirm('软件有更新,是否更新?')){
                		        			console.log(App.eplus_url+data.data.epAppPath);
                		        			$('#loadingPart').show();
                		        			DownLoadFile(App.eplus_url+data.data.epAppPath);
                		        		}
//                	        		}
//                	        		else{
//                	        			//IOS只整包更新
//                	        		}
                	        	}
                	        	else{
                	        		console.log(data.msg);
                	        	}
                });
			}
	  	});
	});

	mui.init({
		gestureConfig: {
			tap: true,
			//默认为true
			doubletap: true,
			//默认为false
			longtap: true,
			//默认为false
			swipe: true,
			//默认为true
			drag: true,
			//默认为true
			hold: false,
			//默认为false，不监听
			release: false
		}
	});
	var first = null;
	mui.back = function() {
		if (!first) {
			first = new Date().getTime();
			mui.toast('再按一次退出软件!');
			setTimeout(function() {
				first = null;
			}, 2000);
		} else {
			if (new Date().getTime() - first < 2000) {
				plus.runtime.quit();
			}
		}
	};



	document.querySelector('body').addEventListener('drag', function(event) {
		if(event.detail.deltaX < -200){
			$('.nav-btn').removeClass('mui-active');
			$('.nav-btn.reg-part').addClass('mui-active');
			$('#login').fadeOut('fast');
			$('#reg').fadeIn('fast');
		}
		else if(event.detail.deltaX > 200){
			$('.nav-btn').removeClass('mui-active');
			$('.nav-btn.login-part').addClass('mui-active');
			$('#reg').fadeOut('fast');
			$('#login').fadeIn('fast');
		}
	});
	
	
});